<template>
  <div>
    <el-card>
      <el-tabs v-model="activeName">
        <el-tab-pane label="登录账户设置" name="first">
          <el-form
            ref="form"
            :rules="rules"
            style="margin:50px auto; width: 400px"
            :model="form"
            label-width="80px"
          >
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username" />
            </el-form-item>
            <el-form-item label="新密码" prop="password">
              <el-input v-model="form.password" type="password" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="save">更新</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="second">
          <user-info />
        </el-tab-pane>
        <el-tab-pane label="岗位信息" name="third">
          <job-info />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { profile, setUserInfoAPI } from '@/api/employees'
// 导入组件
import UserInfo from './components/UserInfo.vue'
import JobInfo from './components/JobInfo.vue'
export default {
  name: 'EmpDetail',
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      activeName: 'first',
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    const res = await profile(this.$route.params.id)
    this.form = res.data
  },
  methods: {
    save() {
      // 整个表单校验
      this.$refs.form.validate(async v => {
        if (v) {
          await setUserInfoAPI(this.form)
          this.$message.success('更新成功')
        }
      })
    }
  }
}
</script>

<style></style>
